#!/usr/bin/env python
# mpc 11/24/2013
# taken from http://raspberrypi.werquin.com/
# before testing, install with
# easy_install watchdogdev
# sudo modprobe bcm2708_wdog
# add line "bcm2708_wdog" to /etc/modules
#
from time import sleep 
import argparse 
import signal
from watchdogdev import *

done = False
def signal_handler(signal, frame):
    print( 'You pressed Ctrl+C!')
    global done
    done = True


def test(sleepSecs):
   global done
   wd = watchdog('/dev/watchdog')
   i = 0
   while(not done):
       print "Send Keep alive"
       i = i+1
       wd.keep_alive()
       for j in range(sleepSecs): #Change to 15 to see RPi reboot ...
           print "... Waiting",j,", Left :",wd.get_time_left()
           sleep(1)
   
   print "Magic Close"        
   wd.magic_close()
   print "Done !"

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='test the hw watchdog.')
    parser.add_argument('--time', dest='sleepSecs', type=int,  default=5,
                       help='time to delay between hitting the wdog (secs), default = 5 ')
        
    args = parser.parse_args()
    print 'arg is %d' % args.sleepSecs
    
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    test(args.sleepSecs)